package com.dindang.propertyfee.mvp.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dindang.propertyfee.R;

/**
 * Created by cxw on & 2018/8/16
 */
public class SpinerPopDateRange extends PopupWindow {
    private LayoutInflater inflater;
    private OnClickListener listener;
    private String[] rangeList = {"今日到期", "明日到期", "7日到期", "30日到期", "已逾期"};

    public SpinerPopDateRange(Context context, OnClickListener listener){
        super(context);
        inflater= LayoutInflater.from(context);
        this.listener = listener;
        init();
    }

    private void init(){
        View view = inflater.inflate(R.layout.propertyfee_pop_date_range, null);
        setContentView(view);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(0));
        setOutsideTouchable(true);
        setFocusable(true);
        RecyclerView recyclerview = view.findViewById(R.id.propertyfee_recyclerview);
        ConstraintLayout cl_bg = view.findViewById(R.id.propertyfee_cl_bg);
        cl_bg.setOnClickListener(v -> dismiss());
        recyclerview.setAdapter(new DateViewAdapter());

    }

    class DateViewAdapter extends RecyclerView.Adapter<DateViewAdapter.ViewHolder> {
        @NonNull
        @Override
        public DateViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.propertyfee_item_date, parent, false);
            DateViewAdapter.ViewHolder viewHolder = new DateViewAdapter.ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull DateViewAdapter.ViewHolder holder, int position) {
            String date = rangeList[position];
            holder.tv_data.setText(date);
            holder.tv_data.setOnClickListener(v -> listener.onClick(date));
        }

        @Override
        public int getItemCount() {
            return rangeList.length;
        }


        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_data;
            ViewHolder(View itemView) {
                super(itemView);
                tv_data = itemView.findViewById(R.id.propertyfee_tv_date);
            }
        }

    }

    public interface OnClickListener{
        void onClick(String rangeName);
    }

}
